home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
UTILITIE
/
CONVERSI
/
0825C.ZIP
/
BIN2HEX.C
< prev
next >
Wrap
Text File
|
1980-01-01
|
4KB
|
176 lines
/*
Binary to HEX-ASCII utility
Makes a binary file into a HEX file, 32 bytes/block with
proper sumcheck byte at the end. The starting address for the
HEX file is settable; the input is assumed to be contiguous
locations. Some garbage may be included at the end of the
HEX file since the program does not recognize 0x1A as an
end-of-file code in the binary input.
Called with:
bin2hex [<binary file 1> [<bin. file 2>...]]
Original version written by:
Robert Pasky
36 Wiswall Rd
Newton Centre, MA 01259
Modified by Edward I. Comer
NOTE: The file opens are modified for C86
*/
#include "stdio.h"
#define DONE 0
#define MORE 1
int i, j, fd;
char doswath = 0;
unsigned int addr,sad,offset,swath;
char fnbuf[30], *fname; /* filename buffer & ptr */
char onbuf[30], *oname; /* output filename buf & ptr */
FILE *ifp, *ofp; /* file pointers */
char hextab[16];
char kybdbuf[20];
char sumcheck;
main(argc,argv)
char **argv;
{
strcpy(hextab,"0123456789ABCDEF");
while (1)
{
doswath = 0;
oname = onbuf;
if (argc-1)
{
fname = *++argv;
argc--;
}
else
{
printf ("\nEnter binary file name: ");
if (!*(fname = gets(fnbuf))) break;
}
strcpy (oname,fname);
striptype(oname);
strcat (oname,".HEX");
if((ifp=fopen(fname,"rb")) == NULL)
{
printf("Can't open %s\n",fname);
continue;
}
else printf("\nReading %-13s",fname);
if((ofp=fopen(oname,"wb")) == NULL)
{
printf("Can't create %s\n",fname);
continue;
}
else printf ("\nWriting %-13s",oname);
printf ("\nStarting address? ");
sscanf (gets(kybdbuf),"%x",&addr);
sad = addr; /* save start address */
printf("\nOffset into %s (in hex bytes) ?",fname);
sscanf(gets(kybdbuf),"%x",&offset);
printf("\nSwath size OR 0 (in hex bytes) ?");
sscanf(gets(kybdbuf),"%x",&swath);
if(swath != 0)
doswath = 1;
/*
** offset into file by desired amount
*/
while(offset)
{
getc(ifp); /* discard byte */
offset--;
}
while(doblock())
;
putc(0xD,ofp);
putc(0xA,ofp);
fclose(ifp);
fclose(ofp);
printf ("\nConversion complete\n");
printf ("\nStart address: %4x",sad);
printf ("\nEnd address: %4x",addr);
}
}
striptype(s)
char *s;
{
while (*s) {
if (*s == '.') *s = '\0';
else s++;
}
}
/* Send a HEX block --
cr,lf,':',count,address,filler,32 bytes of code,sumcheck
return eofflag -- DONE if end of file reached
*/
doblock()
{
int eofflag,c;
putc(0xD,ofp);
putc(0xA,ofp);
putc(':',ofp);
sumcheck = 0;
putbyte(0x20,ofp);
putaddr(addr,ofp);
putbyte(0,ofp);
eofflag = MORE;
for (i=0; i<0x20; i++)
{
if ((c = getc(ifp)) == EOF || (doswath && swath == 0))
{
eofflag = DONE;
c = 0;
}
putbyte(c,ofp);
if(doswath)
swath--;
}
putbyte(-sumcheck,ofp);
addr += 0x20;
if (eofflag == DONE)
{
putc(0xD,ofp);
putc(0xA,ofp);
putc(':',ofp);
putbyte(0,ofp); /* count of 0 is eof signal */
putaddr(0,ofp);
putbyte(0,ofp);
}
return (eofflag);
}
/* Send 1 byte (2 characters);
value of h to file iofp
*/
putbyte(h,iofp)
int h;
FILE *iofp;
{
sumcheck += h;
putc( hextab[(h>>4)&0xF], iofp);
putc( hextab[h&0xF], iofp);
}
/* Send 2 bytes (4 characters)
address value of h to file iofp
*/
putaddr(h,iofp)
int h;
FILE *iofp;
{
putbyte(((h>>8) & 0xFF), iofp);
putbyte(h & 0xFF, iofp);
}